//-----------------------------------------------------------------------
// <summary>
//  The file contains a class that used microsoft patterns and practices composite application guidance for silverlight application
// </summary>
// <copyright file="CallbackLogger.cs" company="DSSoftLogic">
//     Copyright@ DSSoftLogic 2011, Non-Commercial. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Property.Silverlight.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;

    using Microsoft.Practices.Prism.Logging;

    /// <summary>
    /// This CallbackLogger has a logger that holds on to log entries until a callback delegate is set, then plays back log entries and sends new log entries.
    /// </summary> 
    public class CallbackLogger : ILoggerFacade
    {
        ////private readonly ILog m_Logger = LogManager.GetLogger(typeof(Log4NetLogger)); 

        /// <summary>
        /// saved logs field of Logger
        /// </summary>
        private readonly Queue<Tuple<string, Category, Priority>> savedLogs =
            new Queue<Tuple<string, Category, Priority>>();

        /// <summary>
        /// callback field
        /// </summary>
        private Action<string, Category, Priority> callback;

        /// <summary>
        /// Gets or sets the callback to receive logs.
        /// </summary>
        /// <value>An Action&lt;string, Category, Priority&gt; callback.</value>
        public Action<string, Category, Priority> Callback
        {
            get { return this.callback; }
            set { this.callback = value; }
        }

        /// <summary>
        /// Write a new log entry with the specified category and priority.
        /// </summary>
        /// <param name="message">Message body to log.</param>
        /// <param name="category">Category of the entry.</param>
        /// <param name="priority">The priority of the entry.</param>
        public void Log(string message, Category category, Priority priority)
        {
            if (this.Callback != null)
            {
                this.Callback(message, category, priority);
            }
            else
            {
                this.savedLogs.Enqueue(new Tuple<string, Category, Priority>(message, category, priority));
            }
        }

        /// <summary>
        /// Replays the saved logs if the Callback has been set.
        /// </summary>
        public void ReplaySavedLogs()
        {
            if (this.Callback != null)
            {
                while (this.savedLogs.Count > 0)
                {
                    var log = this.savedLogs.Dequeue();
                    TextLogger text = new TextLogger();
                    text.Log(log.Item1, log.Item2, log.Item3);
                    this.Callback(log.Item1, log.Item2, log.Item3);
                }
            }
        }
    }
}
